package com.hontech.bread.ui.parts

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
import android.widget.FrameLayout
import com.hontech.bread.R
import com.hontech.bread.device.deliveryLeft
import com.hontech.bread.device.deliveryRight
import com.hontech.bread.device.reset
import com.hontech.bread.ui.dialog.runProg
import com.hontech.bread.ui.view.LabelInput
import com.hontech.bread.ui.view.LabelSpinner
import com.hontech.bread.utils.onClick
import com.hontech.bread.utils.stringById

class DeliveryParts(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val spRows: LabelSpinner
    private val btnReset: Button
    private val btnLeft: Button
    private val btnRight: Button
    private val liTime: LabelInput

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_delivery, this)

        spRows = view.findViewById(R.id.id_delivery_ls_cargos)
        btnReset = view.findViewById(R.id.id_delivery_btn_reset)
        btnLeft = view.findViewById(R.id.id_delivery_btn_left_delivery)
        btnRight = view.findViewById(R.id.id_delivery_btn_right_delivery)
        liTime = view.findViewById(R.id.id_delivery_li_time)
        btnReset.onClick { onClickReset() }
        btnLeft.onClick { onClickLeft() }
        btnRight.onClick { onClickRight() }
    }

    private suspend fun onClickReset() {
        runProg(
            stringById(R.string.t1733460848_408),
            stringById(R.string.t1733460848_409)
        ) { dialog ->
            reset { dialog.setMsg(it) }
        }
    }

    private suspend fun onClickLeft() {
        runProg(
            stringById(R.string.t1733460848_410),
            "${stringById(R.string.t1733460848_411)}:${spRows.selectText()}"
        ) { dialog ->
            deliveryLeft(spRows.selectText().toInt(), liTime.textInt().toLong() * 1000) {
                dialog.setMsg(it)
            }
        }
    }

    private suspend fun onClickRight() {
        runProg(
            stringById(R.string.t1733460848_412),
            "${stringById(R.string.t1733460848_413)}:${spRows.selectText()}"
        ) { dialog ->
            deliveryRight(spRows.selectText().toInt(), liTime.textInt().toLong() * 1000) {
                dialog.setMsg(it)
            }
        }
    }
}


